package com.orkhonsoft.tengrinshar.chat.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.orkhonsoft.tengrinshar.chat.common.PageUtils;
import com.orkhonsoft.tengrinshar.chat.common.Query;
import com.orkhonsoft.tengrinshar.chat.common.RestCode;
import com.orkhonsoft.tengrinshar.chat.common.RestResponse;
import com.orkhonsoft.tengrinshar.chat.entity.ChatRecordEntity;
import com.orkhonsoft.tengrinshar.chat.service.ChatRecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



/**
 * 聊天记录
 * 
 * @author bain
 * @email 2668013653@qq.com
 * @date 2018-10-29 10:59:16
 */
@Api("聊天记录相关的相关的api")
@RestController
@RequestMapping("chatrecord")
public class ChatRecordController extends AbstractController{
	@Autowired
	private ChatRecordService chatRecordService;
	@GetMapping("list")
	public RestResponse<PageUtils> list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<ChatRecordEntity> list = chatRecordService.queryList(query);
		int total = chatRecordService.queryTotal();
		
		PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return RestResponse.success(page);
	}
	
	@GetMapping("/{id}")
	public RestResponse<ChatRecordEntity> show(@PathVariable("id") Long id) {
		ChatRecordEntity chatRecord = chatRecordService.queryObject(id);
		
		if(chatRecord == null) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		return RestResponse.success(chatRecord);
	}
	
	@PostMapping("")
	public RestResponse<Object> save(ChatRecordEntity chatRecord) {
		chatRecordService.save(chatRecord);
		
		return RestResponse.success();
	}
	
	@PutMapping("/{id}")
	public RestResponse<Object> update(@PathVariable("id") Long id, ChatRecordEntity chatRecord) {
		ChatRecordEntity orgChatRecord = chatRecordService.queryObject(id);
		
		if(orgChatRecord == null) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		chatRecord.setId(id);
		chatRecordService.update(chatRecord);
		
		return RestResponse.success();
	}
	
	@DeleteMapping("/{id}")
	public RestResponse<Object> delete(@PathVariable("id") Long id) {
		
		ChatRecordEntity chatRecord = chatRecordService.queryObject(id);
		
		if(chatRecord == null) {
			return RestResponse.error(RestCode.DATA_NOT_FOUND);
		}
		
		chatRecordService.delete(id);
		
		return RestResponse.success();
	}
	@PostMapping("/test")
	public RestResponse<Object> delete() {
		getUserId();
		return RestResponse.success(getUserId());
	}
	
}
